﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace postSale.forms
{
    public partial class schedule : application.baseDiaglogForm 
    {
        private int[] mySelectedMonths
        {
            get {return schMonthLb.myValue;}
            set {schMonthLb.myValue=value;}
        }
        private int[] mySelectedDays
        {
            get { return schDayLb.myValue; }
            set { schDayLb.myValue = value; }
        }
        private byte myStatus
        {
            get { return cbStatus.SelectedValue; }
            set { cbStatus.SelectedValue = value; }
        }
        private byte myWarningMedia
        {
            get { return warningMediaLb.myValue; }
            set { warningMediaLb.myValue = value; }
        }
        private byte myWarnDays
        {
            get { return (byte)warningTimeEd.Value; }
            set
            {
                if (value >= warningTimeEd.Minimum && value <= warningTimeEd.Maximum) warningTimeEd.Value = value;
            }
        }
        private byte myRepeatWarnTime
        {
            get 
            { 
                return (byte)(repeatWarnTimeEd.Enabled?repeatWarnTimeEd.Value:0); 
            }
            set
            {
                if (value >= repeatWarnTimeEd.Minimum && value <= repeatWarnTimeEd.Maximum) repeatWarnTimeEd.Value = value;
                else repeatWarnTimeEd.Value = repeatWarnTimeEd.Minimum;
                repeatWarnTimeChk.Checked = (value>0);
            }
        }
        private int[] myRecipients
        {
            get 
            {
                int workerId=0;
                int[] list = new int[recipientLb.Items.Count];
                ListViewItem item;
                for (int idx = 0; idx < recipientLb.Items.Count; idx++)
                {
                    item = (ListViewItem)recipientLb.Items[idx];
                    int.TryParse(item.Name,out workerId);
                    list[idx] = workerId;
                }
                return list;
            }
            set 
            {
                recipientLb.Items.Clear();
                if (value == null) return;
                data.masterDS.workerRow workerRow;
                for (int idx = 0; idx < value.Length; idx++)
                {
                    workerRow = DataAccess.DataCache.Find(myMasterDS.worker,value[idx]);
                    if (workerRow == null) continue;
                    AddRecipient(workerRow);
                }
            }
        }
        public schedule()
        {
            try
            {
                InitializeComponent();
                this.myStatusStrip.Visible = true;
                schMonthLb.LoadData();
                schDayLb.LoadData();
                cbStatus.LoadData();
                warningMediaLb.LoadData();
                recipientCountLbl.ForeColor = CommonTypes.Settings.sysColorHiLightFG1;
                warningMediaLb.BackColor = this.BackColor;
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
        }

        private data.postSaleDS.taskScheduleDataTable _scheduleTaskTbl = null;
        private data.postSaleDS.taskScheduleDataTable scheduleTaskTbl
        {
            get
            {
                if (_scheduleTaskTbl == null) _scheduleTaskTbl = new data.postSaleDS.taskScheduleDataTable();
                return _scheduleTaskTbl;
            }
        }
        public void SetInfo(data.postSaleDS.taskScheduleRow mainRow)
        {
            this.mySelectedMonths = postSale.SysLibs.Str2List(mainRow.months);
            this.mySelectedDays = postSale.SysLibs.Str2List(mainRow.days);

            this.myWarningMedia = mainRow.warnMedia;
            this.myWarnDays = mainRow.warnDay;
            this.myStatus = mainRow.status;
            this.myRecipients = postSale.SysLibs.Str2List(mainRow.recipients);
            this.ShowRecipientCount();

            this.myRepeatWarnTime = mainRow.repeatWarnDay;

            sDateChk.Checked = !mainRow.IsstartDateNull();
            if (sDateChk.Checked) sDateEd.myDateTime = mainRow.startDate;
            
            eDateChk.Checked = !mainRow.IsendDateNull();
            if (eDateChk.Checked) eDateEd.myDateTime = mainRow.endDate; 
        }
        public void GetInfo(data.postSaleDS.taskScheduleRow mainRow)
        {
            mainRow.months = postSale.SysLibs.List2String(this.mySelectedMonths);
            mainRow.days = postSale.SysLibs.List2String(this.mySelectedDays);
            
            // Add one more separator at the end to ensure that it can be found by LIKE command 
            mainRow.recipients = postSale.SysLibs.List2String(this.myRecipients);
            mainRow.warnMedia = this.myWarningMedia;
            mainRow.warnDay = this.myWarnDays;
            mainRow.status = this.cbStatus.SelectedValue;
            mainRow.repeatWarnDay = this.myRepeatWarnTime;

            if (sDateChk.Checked)
            {
                mainRow.startDate = sDateEd.myDateTime; 
            }
            else
            {
                mainRow.SetstartDateNull(); 
            }
            if (eDateChk.Checked)
            {
                mainRow.endDate = eDateEd.myDateTime; 
            }
            else
            {
                mainRow.SetendDateNull(); 
            }
        }

        protected override bool BeforeAcceptProcess() 
        {
            string tmp;
            if (this.mySelectedMonths == null)
            {
                common.SysLibs.ShowErrorMessage("[" + schMonthChk.Text + "] không hợp lệ.");
                schMonthLb.Focus();
                return false;
            }
            tmp = common.StringLibs.ToString(this.mySelectedMonths);
            if (tmp.Length > scheduleTaskTbl.monthsColumn.MaxLength)
            {
                common.SysLibs.ShowErrorMessage("Quá nhiều [" + schMonthChk.Text + "].");
                schMonthLb.Focus();
                return false;
            }

            if (this.mySelectedDays == null)
            {
                common.SysLibs.ShowErrorMessage("[" + schDayChk.Text + "] không hợp lệ.");
                schDayLb.Focus();
                return false;
            }
            tmp = common.StringLibs.ToString(this.mySelectedDays);
            if (tmp.Length > scheduleTaskTbl.daysColumn.MaxLength)
            {
                common.SysLibs.ShowErrorMessage("Quá nhiều [" + schDayChk.Text + "].");
                schDayLb.Focus();
                return false;
            }

            if (this.myRecipients.Length ==0)
            {
                common.SysLibs.ShowErrorMessage("[" + recipientLbl.Text + "] không hợp lệ.");
                recipientLb.Focus();
                return false;
            }
            tmp = common.StringLibs.ToString(this.myRecipients);
            if (tmp.Length > scheduleTaskTbl.recipientsColumn.MaxLength)
            {
                common.SysLibs.ShowErrorMessage("Quá nhiều [" + recipientLbl.Text + "].");
                recipientLb.Focus();
                return false;
            }
            if (this.sDateEd.Enabled && this.sDateEd.myDateTime == common.Consts.constNullDate)
            {
                common.SysLibs.ShowErrorMessage("[" + sDateChk.Text + "] không hợp lệ.");
                sDateEd.Focus();
                return false;
            }
            if (this.eDateEd.Enabled && this.eDateEd.myDateTime == common.Consts.constNullDate)
            {
                common.SysLibs.ShowErrorMessage("[" + eDateChk.Text + "] không hợp lệ.");
                eDateEd.Focus();
                return false;
            }
            return true;
        }

        private void DelSelecteRecipients()
        {
            while (recipientLb.SelectedIndices.Count > 0)
                recipientLb.Items.RemoveAt(recipientLb.SelectedIndices[0]);
            recipientLb.Refresh();
            ShowRecipientCount();
        }
        private void AddRecipient(data.masterDS.workerRow workerRow)
        {
            ListViewItem item = new ListViewItem();
            item.Name = workerRow.workerId.ToString();
            item.Text = workerRow.login;
            if (recipientLb.Items.Contains(item)) return;
            recipientLb.Items.Add(item);
        }
        private void ShowRecipientCount()
        {
            recipientCountLbl.Text = "[" + recipientLb.Items.Count.ToString() + "]";
        }

        private void addRecipientBtn_Click(object sender, EventArgs e)
        {
            try
            {
                application.dataFind.myWorkerFind.Find(common.Settings.sysFindMarker.ToString());
                for (int idx = 0; idx < application.dataFind.myWorkerFind.selectedRows.Count; idx++)
                {
                    DataRowView dataRow = (DataRowView)application.dataFind.myWorkerFind.selectedRows[idx].DataBoundItem;
                    AddRecipient((data.masterDS.workerRow)dataRow.Row);
                }
                ShowRecipientCount();
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
        }
        private void delRecipientBtn_Click(object sender, EventArgs e)
        {
            try
            {
                DelSelecteRecipients();
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
        }

        private void schMonthChk_CheckedChanged(object sender, EventArgs e)
        {
            schMonthLb.CheckAll(schMonthChk.Checked);
        }
        private void schDayChk_CheckedChanged(object sender, EventArgs e)
        {
            schDayLb.CheckAll(schDayChk.Checked);
        }
        private void recipientLb_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                DelSelecteRecipients();
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
        }

        private void repeatWarnTimeChk_CheckedChanged(object sender, EventArgs e)
        {
            repeatWarnTimeEd.Enabled = repeatWarnTimeChk.Checked;
        }

        private void sDateChk_CheckedChanged(object sender, EventArgs e)
        {
            sDateEd.Enabled = sDateChk.Checked;
        }

        private void eDateChk_CheckedChanged(object sender, EventArgs e)
        {
            eDateEd.Enabled = eDateChk.Checked;
        }
    }
}
